package com.kk.permission.accessibilityutils;

import android.accessibilityservice.AccessibilityService;
import android.os.Looper;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;

//import com.cootek.dialer.base.baseutil.thread.UiThreadExecutor;

public final class NodeUtil {
    private static final String TAG = "NodeUtil";
    private static final String[] switchNames = new String[]{"android.widget.Switch", "android.support.v7.widget.SwitchCompat", "android.widget.CheckBox"};
    private static final Set<String> switcherSet = new HashSet(Arrays.asList(switchNames));

    public static void recycle(AccessibilityNodeInfo accessibilityNodeInfo) {
    }

    public static boolean clickByText(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        return clickByText(accessibilityNodeInfo, str, 3);
    }

    public static boolean clickByText(AccessibilityNodeInfo accessibilityNodeInfo, String str, int i) {
        if (IsUtil.isNonNull(accessibilityNodeInfo) && IsUtil.isNotEmpty(str)) {
            List<AccessibilityNodeInfo> findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
            if (IsUtil.isNotEmpty(findAccessibilityNodeInfosByText)) {
                AccessibilityNodeInfo accessibilityNodeInfo2 = null;
                if (findAccessibilityNodeInfosByText.size() > 1) {
                    for (AccessibilityNodeInfo accessibilityNodeInfo3 : findAccessibilityNodeInfosByText) {
                        if (accessibilityNodeInfo3.isClickable()) {
                            accessibilityNodeInfo2 = accessibilityNodeInfo3;
                            break;
                        }
                    }
                }
                if (accessibilityNodeInfo2 == null) {
                    accessibilityNodeInfo2 = findAccessibilityNodeInfosByText.get(0);
                }
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("clickable = ");
                stringBuilder.append(accessibilityNodeInfo2.isClickable());
                if (accessibilityNodeInfo2.isClickable()) {
                    return click(accessibilityNodeInfo2, true);
                }
                for (int i2 = 0; i2 < i; i2++) {
                    if (IsUtil.isNonNull(accessibilityNodeInfo2)) {
                        AccessibilityNodeInfo parent;
                        try {
                            parent = accessibilityNodeInfo2.getParent();
                        } catch (Throwable e) {
                            parent = accessibilityNodeInfo2;
                        }
                        recycle(accessibilityNodeInfo2);
                        if (IsUtil.isClickable(parent)) {
                            return click(parent);
                        }
                        accessibilityNodeInfo2 = parent;
                    }
                }
            }
        }
        return false;
    }

    public static boolean performSwitch(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        boolean z;
        List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("--------------hahha22 -- ");
        stringBuilder.append(findAccessibilityNodeInfosByText.size());
        if (IsUtil.isNotEmpty(findAccessibilityNodeInfosByText)) {
            z = false;
            for (int i = 0; i < findAccessibilityNodeInfosByText.size(); i++) {
                AccessibilityNodeInfo switchableNode = getSwitchableNode((AccessibilityNodeInfo) findAccessibilityNodeInfosByText.get(i));
                if (switchableNode != null) {
                    z = click(switchableNode);
                    String str2 = TAG;
                    StringBuilder stringBuilder2 = new StringBuilder();
                    stringBuilder2.append("performSwitch res");
                    stringBuilder2.append(z);
                    recycle(switchableNode);
                    if (z) {
                        break;
                    }
                }
            }
        } else {
            z = false;
        }
        recycleAll(findAccessibilityNodeInfosByText);
        recycle(accessibilityNodeInfo);
        return z;
    }

    public static boolean performSwitchMate10(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
        int i = 0;
        if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
            return false;
        }
        accessibilityNodeInfo = (AccessibilityNodeInfo) findAccessibilityNodeInfosByText.get(0);
        int childCount = accessibilityNodeInfo.getChildCount();
        boolean z = false;
        while (i < childCount) {
            z = click(accessibilityNodeInfo.getChild(i));
            if (z) {
                return z;
            }
            i++;
        }
        return z;
    }

    public static AccessibilityNodeInfo getScrollableNode(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (isScrollView(accessibilityNodeInfo)) {
            return accessibilityNodeInfo;
        }
        Queue linkedList = new LinkedList();
        linkedList.add(accessibilityNodeInfo);
        while (!linkedList.isEmpty()) {
            accessibilityNodeInfo = (AccessibilityNodeInfo) linkedList.poll();
            if (isScrollView(accessibilityNodeInfo)) {
                return accessibilityNodeInfo;
            }
            int i = 0;
            while (accessibilityNodeInfo != null && i < accessibilityNodeInfo.getChildCount()) {
                linkedList.add(accessibilityNodeInfo.getChild(i));
                i++;
            }
        }
        return null;
    }

    private static boolean isScrollView(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (accessibilityNodeInfo == null || IsUtil.isNull(accessibilityNodeInfo.getClassName())) {
            return false;
        }
        String charSequence = accessibilityNodeInfo.getClassName().toString();
        String str = TAG;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("className ");
        stringBuilder.append(charSequence);
        if (!charSequence.equals("android.widget.ListView") && !charSequence.equals("android.widget.GridView") && !charSequence.toLowerCase().contains("recyclerview") && !charSequence.toLowerCase().contains("scrollview") && !charSequence.toLowerCase().contains("expandablelistview")) {
            return false;
        }
        return true;
    }

    public static AccessibilityNodeInfo getSwitchableNode(AccessibilityNodeInfo accessibilityNodeInfo) {
        while (accessibilityNodeInfo != null) {
            AccessibilityNodeInfo switchRecursive = getSwitchRecursive(accessibilityNodeInfo);
            if (switchRecursive != null) {
                return switchRecursive;
            }
            switchRecursive = accessibilityNodeInfo.getParent();
            recycle(accessibilityNodeInfo);
            accessibilityNodeInfo = switchRecursive;
        }
        return null;
    }

    private static AccessibilityNodeInfo getSwitchRecursive(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (IsUtil.isNotNullClassName(accessibilityNodeInfo) && switcherSet.contains(accessibilityNodeInfo.getClassName().toString())) {
            return accessibilityNodeInfo;
        }
        if (accessibilityNodeInfo == null || accessibilityNodeInfo.getChildCount() == 0) {
            return null;
        }
        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
            AccessibilityNodeInfo switchRecursive = getSwitchRecursive(accessibilityNodeInfo.getChild(i));
            if (switchRecursive != null) {
                return switchRecursive;
            }
        }
        return null;
    }

    public static boolean pageContains(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        if (accessibilityNodeInfo == null) {
            return false;
        }
        List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
        recycleAll(findAccessibilityNodeInfosByText);
        return IsUtil.isNotEmpty(findAccessibilityNodeInfosByText);
    }

    private static void recycleAll(List<AccessibilityNodeInfo> list) {
        if (!IsUtil.isEmpty((List) list)) {
            for (AccessibilityNodeInfo recycle : list) {
                recycle(recycle);
            }
        }
    }

    public static void click(final AccessibilityNodeInfo accessibilityNodeInfo, int i) {
        if (i <= 0) {
            i = 200;
        }
        if (accessibilityNodeInfo == null) {
        } else if (accessibilityNodeInfo.isClickable()) {
            try {
                new android.os.Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        accessibilityNodeInfo.performAction(16);
                    }
                });
//                UiThreadExecutor.execute(new Runnable() {
//                    public void run() {
//                        accessibilityNodeInfo.performAction(16);
//                    }
//                }, (long) i);
            } catch (Exception e) {
                String str = TAG;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("Click e ");
                stringBuilder.append(e.getMessage());
            } catch (Throwable th) {
                recycle(accessibilityNodeInfo);
            }
            recycle(accessibilityNodeInfo);
        } else {
        }
    }

    public static boolean click(AccessibilityNodeInfo accessibilityNodeInfo) {
        return click(accessibilityNodeInfo, false, false);
    }

    public static boolean click(AccessibilityNodeInfo accessibilityNodeInfo, boolean z) {
        return click(accessibilityNodeInfo, false, z);
    }

    /* JADX WARNING: Failed to extract finally block: empty outs */
    public static boolean click(android.view.accessibility.AccessibilityNodeInfo nodeInfo, boolean r5, boolean r6) {
        if(nodeInfo.isClickable())
            nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        /*
        r0 = 0;
        if (r4 == 0) goto L_0x0056;
    L_0x0003:
        if (r5 != 0) goto L_0x0016;
    L_0x0005:
        r5 = r4.isClickable();
        if (r5 == 0) goto L_0x000c;
    L_0x000b:
        goto L_0x0016;
    L_0x000c:
        r4 = "NodeUtil";
        r5 = "This nodeInfo can not be clicked.";
        r6 = new java.lang.Object[r0];
        com.cootek.base.tplog.TLog.i(r4, r5, r6);
        goto L_0x005f;
    L_0x0016:
        r5 = 16;
        r5 = r4.performAction(r5);	 Catch:{ Exception -> 0x002b }
        if (r6 == 0) goto L_0x0024;
    L_0x001e:
        if (r5 == 0) goto L_0x0027;
    L_0x0020:
        recycle(r4);
        goto L_0x0027;
    L_0x0024:
        recycle(r4);
    L_0x0027:
        r0 = r5;
        goto L_0x005f;
    L_0x0029:
        r5 = move-exception;
        goto L_0x004f;
    L_0x002b:
        r5 = move-exception;
        r1 = "NodeUtil";
        r2 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0029 }
        r2.<init>();	 Catch:{ all -> 0x0029 }
        r3 = "Click e ";
        r2.append(r3);	 Catch:{ all -> 0x0029 }
        r5 = r5.getMessage();	 Catch:{ all -> 0x0029 }
        r2.append(r5);	 Catch:{ all -> 0x0029 }
        r5 = r2.toString();	 Catch:{ all -> 0x0029 }
        r2 = new java.lang.Object[r0];	 Catch:{ all -> 0x0029 }
        com.cootek.base.tplog.TLog.e(r1, r5, r2);	 Catch:{ all -> 0x0029 }
        if (r6 == 0) goto L_0x004b;
    L_0x004a:
        goto L_0x005f;
    L_0x004b:
        recycle(r4);
        goto L_0x005f;
    L_0x004f:
        if (r6 == 0) goto L_0x0052;
    L_0x0051:
        goto L_0x0055;
    L_0x0052:
        recycle(r4);
    L_0x0055:
        throw r5;
    L_0x0056:
        r4 = "NodeUtil";
        r5 = "nodeInfo is null!!";
        r6 = new java.lang.Object[r0];
        com.cootek.base.tplog.TLog.e(r4, r5, r6);
    L_0x005f:
        return r0;
        */
//        throw new UnsupportedOperationException("Method not decompiled: com.cootek.permission.accessibilityutils.NodeUtil.click(android.view.accessibility.AccessibilityNodeInfo, boolean, boolean):boolean");
        return r5;
    }

    public static boolean scrollBackward(AccessibilityNodeInfo accessibilityNodeInfo) {
        return scroll(accessibilityNodeInfo, 1, false);
    }

    @Deprecated
    public static boolean scrollForward(AccessibilityNodeInfo accessibilityNodeInfo) {
        return scroll(accessibilityNodeInfo, 0, true);
    }

    public static boolean findScrollableNodeAndScrollForward(AccessibilityNodeInfo accessibilityNodeInfo) {
        return scroll(getScrollableNode(accessibilityNodeInfo), 0, true);
    }

    public static boolean scrollThisNodeForward(AccessibilityNodeInfo accessibilityNodeInfo) {
        return scroll(accessibilityNodeInfo, 0, true);
    }

    public static boolean forceScrollForward(AccessibilityNodeInfo accessibilityNodeInfo) {
        return scroll(accessibilityNodeInfo, 0, true);
    }

    private static boolean scroll(AccessibilityNodeInfo accessibilityNodeInfo, int i, boolean z) {
        boolean z2 = false;
        String str = TAG;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("scroll direc->");
        stringBuilder.append(i);
        stringBuilder.append(" force->");
        stringBuilder.append(z);
        stringBuilder.append("nodeinfo is null->");
        stringBuilder.append(accessibilityNodeInfo == null);
        i = i == 0 ? 4096 : 8192;
        if (accessibilityNodeInfo == null) {
            z2 = false;
        } else if (z || IsUtil.isScrollable(accessibilityNodeInfo)) {
            try {
                z2 = accessibilityNodeInfo.performAction(i);
            } catch (Exception e) {

            } finally {
                recycle(accessibilityNodeInfo);
            }
        } else {
            throw new RuntimeException("This nodeInfo can not be scrolled.");
        }
        String str3 = TAG;
        StringBuilder stringBuilder3 = new StringBuilder();
        stringBuilder3.append("return ");
        stringBuilder3.append(z2);
        return z2;
    }

    public static boolean back(AccessibilityService accessibilityService) {
        if (accessibilityService != null) {
            try {
                return accessibilityService.performGlobalAction(1);
            } catch (Exception e) {
                String str = TAG;
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("e ");
                stringBuilder.append(e.getMessage());
            }
        } else {
            return false;
        }
        return false;
    }

    public static void back(final AccessibilityService accessibilityService, int i) {
        if (i <= 0) {
            i = 200;
        }
        if (accessibilityService != null) {
            new android.os.Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    try {
                        accessibilityService.performGlobalAction(1);
                    } catch (Exception e) {
                        String str = NodeUtil.TAG;
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("e ");
                        stringBuilder.append(e.getMessage());
                    }
                }
            });
            new android.os.Handler().post(new Runnable() {
                @Override
                public void run() {
                    try {
                        accessibilityService.performGlobalAction(1);
                    } catch (Exception e) {
                        String str = NodeUtil.TAG;
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append("e ");
                        stringBuilder.append(e.getMessage());
                    }
                }
            });
        } else {
        }
    }
}
